<template>
  <div class="app-container">
    <el-col style="width: 100%; align-items:center;" type="flex" justify="space-between">
      <el-row>
        <el-button type="text" icon="el-icon-arrow-left" size="large" v-on:click="backHistory">返回上级</el-button>
      </el-row>
      <el-row>
        <div style="margin: 4px 0px 20px 4px;">
          <span style="font-size: 20px; font-weight: bold">用户信息</span>
        </div>
      </el-row>
      <el-card type="box-card" style="padding-left: 20px; padding-top: 20px;padding-bottom: 20px">
        <el-form ref="form" :model="form" label-position="left">
          <el-row>
            <el-col :span="8">
              <el-avatar shape="square" size="large" :src="form.squareUrl"/>
            </el-col>
            <el-col :span="8">
              <el-upload
                class="upload-demo"
                action="https://jsonplaceholder.typicode.com/posts/"
                :on-preview="handlePreview"
                multiple
                :limit="3"
              >
                <el-button size="small" icon="el-icon-upload2" plain>上传头像</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
              </el-upload>
            </el-col>
          </el-row>
          <el-row style="margin-top: 30px">
            <el-col :span="8">
              <span style="font-weight: bold">工号</span>
            </el-col>
            <el-col :span="8" style="color: #5a5e66">
              <span>000000</span>
            </el-col>
          </el-row>
          <el-row style="margin-top: 30px">
            <el-col :span="8">
              <span style="font-weight: bold">姓名</span>
            </el-col>
            <el-col :span="8" style="color: #5a5e66">
              <span>{{$route.query.name}}</span>
            </el-col>
          </el-row>
          <el-row style="margin-top: 30px">
            <el-col :span="8">
              <span style="font-weight: bold">所属部门</span>
            </el-col>
            <el-col :span="8" style="color: #5a5e66">
              <span>产品研发部</span>
            </el-col>
          </el-row>
          <el-row style="margin-top: 30px">
            <el-col :span="8">
              <span style="font-weight: bold">手机号码</span>
            </el-col>
            <el-col :span="8" style="color: #5a5e66">
              <span>12345678</span>
            </el-col>
            <el-col :span="8" style="color: #5a5e66">
              <el-button size="small" icon="el-icon-edit">更改</el-button>
            </el-col>
          </el-row>
          <el-row style="margin-top: 30px">
            <el-col :span="8">
              <span style="font-weight: bold">邮箱</span>
            </el-col>
            <el-col :span="8" style="color: #5a5e66">
              <span>zhangsan@163.com</span>
            </el-col>
            <el-col :span="8" style="color: #5a5e66">
              <el-button size="small" icon="el-icon-edit" @click="changeEmail">更改</el-button>
            </el-col>
          </el-row>
          <el-row style="margin-top: 30px;">
            <el-col :span="8">
              <span style="font-weight: bold">账户密码</span>
            </el-col>
            <el-col :span="8" style="color: #5a5e66">
              <span>已设置</span>
            </el-col>
            <el-col :span="8" style="color: #5a5e66">
              <el-button size="small" icon="el-icon-edit" @click="dialogFormVisible = true">更改</el-button>
              <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
                <el-form ref="ruleForm" :model="ruleForm" status-icon :rules="rules" label-width="100px"
                         class="demo-ruleForm">
                  <el-form-item label="输入旧密码" prop="pass">
                    <el-input v-model="ruleForm.oldPass" type="password" autocomplete="off"/>
                  </el-form-item>
                  <el-form-item label="设置新密码" prop="pass">
                    <el-input v-model="ruleForm.pass" type="password" autocomplete="off"/>
                  </el-form-item>
                  <el-form-item label="确认新密码" prop="checkPass">
                    <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off"/>
                  </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                  <el-button @click="dialogFormVisible = false">取 消</el-button>
                  <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
                </div>
              </el-dialog>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </el-col>
  </div>
</template>

<script>

export default {
  name: 'MyInfo',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        squareUrl: 'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png',
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      dialogFormVisible: false,
      ruleForm: {
        oldPass: '',
        pass: '',
        checkPass: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    backHistory() {
      this.$router.go(-1)
    },
    changeEmail() {
      this.$prompt('请输入邮箱', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
        inputErrorMessage: '邮箱格式不正确'
      }).then(({ value }) => {
        this.$message({
          type: 'success',
          message: '你的邮箱是: ' + value
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
